//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite.Simulation
{
    public class Polygon : CollisionShape
    {
        Collection<Vector2> points = new Collection<Vector2>();

        public Polygon()
        {
        }

        public Polygon(IEnumerable<Vector2> points)
        {
            foreach (Vector2 point in points)
                Points.Add(point);
        }

        public Collection<Vector2> Points
        {
            get { return points; }
        }

        public override ShapeType ShapeType
        {
            get { return ShapeType.Polygon; }
        }

        public override BoundingRectangle GetBounds(Transform2D t)
        {
            if (Points.Count == 0)
                return BoundingRectangle.Empty;

            Vector2 min = new Vector2(float.MaxValue);
            Vector2 max = new Vector2(float.MinValue);

            
            foreach (Vector2 vertex in Points)
            {
                Vector2 v = t.TransformPoint(vertex);
                min = Vector2.Min(min, v);
                max = Vector2.Max(max, v);
            }

            return new BoundingRectangle(min, max);
        }

        public override Vector2 SupportMapping(Vector2 v)
        {
            Vector2 supVec = Vector2.Zero;
            float maxDot = float.MinValue;

            for (int i = 0; i < Points.Count; i++)
            {
                float dot = Vector2.Dot(Points[i], v);

                if (dot > maxDot)
                {
                    supVec = Points[i];
                    maxDot = dot;
                }
            }

            return supVec;
        }
    }
}
